﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;
using System.Net;
using System.Data;

namespace Help
{
    public class cookieHelp
    {
        #region 用户基本属性
        /// <summary>
        /// 用户Id
        /// </summary>
        public static string AdminId
        {
            get
            {
                try
                {
                    if (HttpContext.Current.Request.Cookies["Admin"] != null)
                    {
                        return HttpContext.Current.Request.Cookies["Admin"]["AdminId"].ToString();
                    }
                    else
                    {
                        return "";
                    }
                }
                catch (Exception ex)
                {
                    throw ex;
                }
            }
        }
        public static string AdminName
        {
            get
            {
                try
                {
                    if (HttpContext.Current.Request.Cookies["Admin"] != null)
                    {
                        return HttpContext.Current.Request.Cookies["Admin"]["AdminName"].ToString();
                    }
                    else
                    {
                        return "";
                    }
                }
                catch (Exception ex)
                {
                    throw ex;
                }
            }
        }

        #endregion

        #region cookie操作
        /// <summary>
        /// 登录信息写入Cookie
        /// </summary>
        /// <param name="userId"></param>
        /// <param name="no"></param>
        /// <param name="userName"></param>
        public static void SetCookies(string AdminId, string AdminName)
        {
            HttpContext.Current.Response.Cookies["Admin"].Expires = DateTime.Now.AddDays(1);
            HttpContext.Current.Response.Cookies["Admin"]["AdminId"] = AdminId;
            HttpContext.Current.Response.Cookies["Admin"]["AdminName"] = AdminName;
        }

        /// <summary>
        /// 登出清楚cookie
        /// </summary>
        public static void ClearCookies()
        {
            HttpContext.Current.Response.Cookies["Admin"].Expires = DateTime.Now.AddDays(-1);
            HttpContext.Current.Response.Cookies["Admin"]["AdminId"] = string.Empty;
            HttpContext.Current.Response.Cookies["Admin"]["AdminName"] = string.Empty;
        }

        #endregion
    }
}
